export function getNowDatetime(dayNum = 0) {
  var newDate
  if (dayNum) {
    newDate = new Date(new Date().getTime() + dayNum * 24 * 60 * 60 * 1000)
  } else {
    newDate = new Date()
  }
  return renderTimeText(newDate)
}

export function renderTimeText(newDate) {
  var year = newDate.getFullYear()
  var mon = newDate.getMonth() + 1
  var day = newDate.getDate()
  var hour = newDate.getHours()
  var min = newDate.getMinutes()
  var sec = newDate.getSeconds()
  mon = mon < 10 ? '0' + mon : mon
  day = day < 10 ? '0' + day : day
  hour = hour < 10 ? '0' + hour : hour
  min = min < 10 ? '0' + min : min
  sec = sec < 10 ? '0' + sec : sec
  return year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec
}

// 格式化年月日时分
export function getYeartime(Data) {
  console.log(Data, 'Data')
  var year = Data.getFullYear()
  var mon = Data.getMonth() + 1
  var day = Data.getDate()
  mon = mon < 10 ? '0' + mon : mon
  day = day < 10 ? '0' + day : day
  return year + '-' + mon + '-' + day
}

export function getTheDateTime(date, number) {
  var timeStamp = date.getTime() + 24 * 60 * 60 * 1000 * number
  var newDate = new Date(timeStamp)
  var year = newDate.getFullYear()
  var mon = newDate.getMonth() + 1
  var day = newDate.getDate()
  var hour = newDate.getHours()
  var min = newDate.getMinutes()
  var sec = newDate.getSeconds()
  mon = mon < 10 ? '0' + mon : mon
  day = day < 10 ? '0' + day : day
  hour = hour < 10 ? '0' + hour : hour
  min = min < 10 ? '0' + min : min
  sec = sec < 10 ? '0' + sec : sec
  return year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec
}

export function getDatetime(date, dayNum = 0) {
  var newDate
  if (dayNum) {
    newDate = date(date().getTime() + dayNum * 24 * 60 * 60 * 1000)
  } else {
    newDate = new Date()
  }
  var year = newDate.getFullYear()
  var mon = newDate.getMonth() + 1
  var day = newDate.getDate()
  var hour = newDate.getHours()
  var min = newDate.getMinutes()
  var sec = newDate.getSeconds()
  mon = mon < 10 ? '0' + mon : mon
  day = day < 10 ? '0' + day : day
  hour = hour < 10 ? '0' + hour : hour
  min = min < 10 ? '0' + min : min
  sec = sec < 10 ? '0' + sec : sec
  return year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec
}

export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach((keys) => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

export function filterIdCard(value) {
  if (value) {
    if (typeof value === 'string') {
      if (value.length === 18) {
        return value.replace(/^(.{6})(?:\d+)(.{6})$/, '$1******$2')
      } else {
        return value
      }
    } else {
      return value
    }
  } else {
    return value
  }
}

export function filterTel(value) {
  if (value) {
    if (typeof value === 'string') {
      if (value.length === 11) {
        return value.replace(/^(.{3})(?:\d+)(.{4})$/, '$1****$2')
      } else {
        return value
      }
    } else {
      return value
    }
  } else {
    return value
  }
}

export function filterText(e) {
  if (e) {
    let replaceLength = e.length > 3 ? e.length / 2 : 1
    let start = Math.ceil(e.length / 2) - replaceLength / 2
    return e.substring(0, start) + ''.padStart(replaceLength, '*') + e.substr(start + replaceLength)
  } else {
    return e
  }
}

/**
 * 数组去重
 * @param {*} arr 数组
 * @param {*} uniId 去重字段
 * @returns
 */
export function uniqueArray(arr, uniId) {
  const res = new Map()
  return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1))
}

/**
 * 获取数字数组中的最大值
 * @param {*} arr
 */
export function getMaxCount(arr) {
  let num = arr[0]
  for (let i = 0, length = arr.length; i < length; i++) {
    if (arr[i] > num) num = arr[i]
  }

  return num
}

/**
 * 获取最近6个月的月份数组
 * @param { Date } date
 * @returns
 */
export function getNearSixMonth(date) {
  let Arr = []
  let monthArr = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
  let nowMonth = date.getMonth() + 1
  if (nowMonth >= 7) {
    Arr = [
      `${nowMonth - 6}月`,
      `${nowMonth - 5}月`,
      `${nowMonth - 4}月`,
      `${nowMonth - 3}月`,
      `${nowMonth - 2}月`,
      `${nowMonth - 1}月`
    ]
  } else {
    let newIdx = nowMonth + 6
    Arr = [
      `${monthArr[newIdx - 6]}月`,
      `${monthArr[newIdx - 5]}月`,
      `${monthArr[newIdx - 4]}月`,
      `${monthArr[newIdx - 3]}月`,
      `${monthArr[newIdx - 2]}月`,
      `${monthArr[newIdx - 1]}月`
    ]
  }
  return Arr
}

/**
 * 根据信息获取默认头像
 * @param {*} data
 * @returns
 */
export const getHeadDefaultImg = (item) => {
  let manImg = require('@/assets/img/threeDMapConsole/icon_man.png')
  let womanImg = require('@/assets/img/threeDMapConsole/icon_woman.png')
  if (item.headimgurl) {
    return item.headimgurl
  } else if (item.idcard) {
    let length = item.idcard.length
    let num
    if (length == 15) {
      num = item.idcard.charAt(14)
    } else {
      num = item.idcard.charAt(16)
    }
    if (num % 2 == 0) {
      return womanImg
    } else {
      return manImg
    }
  } else if (item.gender) {
    if (item.gender == 1 || item.gender == '男') return manImg
    else if (item.gender == 2 || item.gender == '女') return womanImg
  } 
  return manImg
}

/**
 * 根据身份证号获取性别
 * @param {*} idcard
 */
export const getGenderByIdcard = (idcard) => {
  if(!idcard) return '未知'
  let gender, sex
  if (idcard.length > 17) {
    sex = idcard.substr(16, 1)
  } else {
    sex = idcard.substr(14, 1)
  }
  if (sex % 2 == 0) gender = '女'
  else gender = '男'
  return gender
}
